package com.see.client;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.see.domain.Order;

public class OrderTableModel extends AbstractTableModel {

	private String[] columnNames = new String[]
            {"ID", "Product", "Amount",  "Match",
            "Operation", "Type", "Limit", "MatchPrice", "ConterParty"};

	private List<Order> orders;
	
	private final static int ID         = 0;
    private final static int PRODUCT    = 1;
    private final static int AMOUNT     = 2;
    private final static int MATCH      = 3;
    private final static int OPERATION  = 4;
    private final static int TYPE       = 5;
    private final static int LIMITPRICE = 6;
    private final static int MATCHRICE  = 7;
    private final static int COUNTERPARTY = 8;
    
    public OrderTableModel() {
    	orders = new ArrayList<Order>(); 
    }
    
    public void addOrder(Order order) {
        orders.add(order);
        int firstRow = orders.size() - 1;
        int lastRow = firstRow;
        fireTableRowsInserted(firstRow, lastRow);
    }
    
	public boolean updateOrder(Order order) {
		boolean updated = false;
		int orderID = order.getId();
		for (Order iterorder : orders) {
			if(iterorder.getId() == orderID){
				iterorder.setMatch(true);
				iterorder.setMatchprice(order.getMatchprice());
				iterorder.setCounterparty(order.getCounterparty());
				updated = true;
				break;
			}
		}
		fireTableRowsUpdated(0, orders.size());
		return updated;
	}
    

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return orders.size();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public boolean isCellEditable(int row, int col) {
        return false;
    }
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Order order = (Order)orders.get(new Integer(rowIndex));
        switch(columnIndex) {
        case ID:
            return order.getId();
        case PRODUCT:
            return order.getProduct();
        case AMOUNT:
            return new Long(order.getAmount());
        case MATCH:
            return order.isMatch();
        case OPERATION:
            return order.getType();
        case TYPE:
            return order.getPrice().isMarket();
        case LIMITPRICE:
            return order.getPrice().getValue();
        case MATCHRICE:
            return order.getMatchprice().toString();
        case COUNTERPARTY:
            return order.getCounterparty();

        }
        return new String();
    }

}

